﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Brain
{
    public class DefaultScreenManager : IScreenManager
    {
        public List<GameScreen> Screens;

        public bool ShouldUpdate { get; set; }
        public bool ShouldDraw { get; set; }

        public DefaultScreenManager()
        {
            Screens = new List<GameScreen>();

            ShouldDraw = true;
            ShouldUpdate = true;
        }

        public virtual void AddScreen(GameScreen screen)
        {
            this.Screens.Add(screen);
        }
        public virtual void RemoveScreen(GameScreen screen)
        {
            this.Screens.Remove(screen);
        }

        public virtual void Reset()
        {
            if (Screens != null)
            {
                Screens = new List<GameScreen>();
            }
        }
        public virtual void Dispose()
        {
            if (Screens != null)
            {
                Screens = null;
            }
        }

        public virtual void Update()
        {
            for (int i = 0; i < Screens.Count; i++)
            {
                GameScreen screen = Screens[i];
                if (screen.ShouldUpdate)
                {
                    screen.Update();
                }
            }
        }

        public virtual void Draw()
        {
            for (int i = 0; i < Screens.Count; i++)
            {
                GameScreen screen = Screens[i];
                if (Engine.Instance.RenderManager3D != null)
                {
                    Engine.Instance.RenderManager3D.StartDraw(screen);
                }
                if (Engine.Instance.RenderManager2D != null)
                {
                    Engine.Instance.RenderManager2D.StartDraw(screen);
                }
                if (screen.ShouldDraw)
                {
                    screen.Draw();
                }
                if (Engine.Instance.RenderManager2D != null)
                {
                    Engine.Instance.RenderManager2D.FinishDraw(screen);
                }
                if (Engine.Instance.RenderManager3D != null)
                {
                    Engine.Instance.RenderManager3D.FinishDraw(screen);
                }
            }
        }


        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
            Data = new SerializationData();

            Data.AddData("DSM.Screens", this.Screens.Count);

            for (int i = 0; i < Screens.Count; i++)
            {
                GameScreen s = Screens[i];
                Data.AddData("DSM.Screens" + i, s.GetType().ToString());
                s.SerializeData(Data, i);
            }
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
            int count = newData.GetData<int>("DSM.Screens");

            for (int i = 0; i < count; i++)
            {
                string t = newData.GetData<string>("DSM.Screens" + i);
                Type type = Type.GetType(t);
                object ob = Activator.CreateInstance(type);
                GameScreen screen = ob as GameScreen;

                this.AddScreen(screen);

                screen.DeserializeData(newData, i);
            }
        }

        public GameScreen GetScreen(int index)
        {
            return Screens[index];
        }
        public int Count()
        {
            return Screens.Count;
        }
    }
}
